package com.forever7776.life.web.controller.api;

import com.forever7776.life.core.service.api.CaptchaService;
import com.forever7776.life.remote.common.consants.PlaceHoldersConstants;
import com.forever7776.life.remote.common.vo.ResultVO;
import com.forever7776.life.remote.rpc.SendEmailRpcService;
import com.forever7776.sendcloud.remote.common.vo.SendCloudEmailParamVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送邮件
 *
 * @author kz
 * @date 2017-12-19
 */
@Controller
@RequestMapping("/api/send/email")
public class SendEmailController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SendEmailRpcService sendEmailRpcService;
    @Autowired
    private CaptchaService captchaService;

    /**
     * 发送html格式的邮件
     *
     * @param emails
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
    @ResponseBody
    public com.forever7776.sendcloud.remote.common.vo.ResultVO index(@RequestParam(value = "emails") String emails,
                                                                     @RequestParam("type") String type,
                                                                     HttpServletRequest request,
                                                                     HttpServletResponse response) {
        String code = captchaService.getCode(emails, request, response);
        com.forever7776.sendcloud.remote.common.vo.ResultVO resultVO = new com.forever7776.sendcloud.remote.common.vo.ResultVO();
        if (code == null) {
            logger.info("验证码生成失败");
            resultVO.setMsg("验证码生成失败");
            return resultVO;
        }
        Map<String, String> map = new HashMap<>(1);
        map.put(PlaceHoldersConstants.CODE, code);
        SendCloudEmailParamVO vo = sendEmailRpcService.createSendEmailVo(null, emails, type);
        vo.setPlaceholders(map);
        resultVO = sendEmailRpcService.sendEmail(vo);
        if (resultVO.getCode().equals(200)) {
            logger.info("发送邮件成功，{}", emails);
        } else {
            logger.info("发送邮件失败，原因：{}", resultVO.getMsg());
        }
        return resultVO;
    }


    /**
     * 发送带附件的邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param filePath
     */
    public ResultVO sendAttachmentsMail(String to, String subject, String content, String filePath) {
        return sendEmailRpcService.sendAttachmentsMail(to, subject, content, filePath);
    }

    /**
     * 发送嵌入静态资源（一般是图片）的邮件
     *
     * @param to
     * @param subject
     * @param content 邮件内容，需要包括一个静态资源的id，比如：<img src=\"cid:rscId01\" >
     * @param rscPath 静态资源路径和文件名
     * @param rscId   静态资源id
     */
    public ResultVO sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
        return sendEmailRpcService.sendInlineResourceMail(to, subject, content, rscPath, rscId);
    }

}
